<template>
  <el-dialog 
    v-model="dialogVisible" 
    :title="(formData.id?'编辑':'添加')+'收货地址'" 
    v-model:visible="dialogVisible" 
    width="50%"
  >
    <div class="wrapper">
      <div class="address-edit">
        <div class="xtx-form">
          <div class="xtx-form-item">
            <div class="label">收货人：</div>
            <div class="field">
              <input v-model="formData.receiver" class="input" placeholder="请输入收货人" />
            </div>
          </div>
          <div class="xtx-form-item">
            <div class="label">手机号：</div>
            <div class="field">
              <input v-model="formData.contact" class="input" placeholder="请输入手机号" />
            </div>
          </div>
          <div class="xtx-form-item">
            <div class="label">地区：</div>
            <div class="field">
              <City 
                placeholder="请选择所在地区"
                :fullLocation="formData.fullLocation"
                @change="changeCty"
              />
            </div>
          </div>
          <div class="xtx-form-item">
            <div class="label">详细地址：</div>
            <div class="field">
              <input  v-model="formData.address" class="input" placeholder="请输入详细地址" />
            </div>
          </div>
          <div class="xtx-form-item">
            <div class="label">邮政编码：</div>
            <div class="field">
              <input v-model="formData.postalCode" class="input" placeholder="请输入邮政编码" />
            </div>
          </div>
          <div class="xtx-form-item">
            <div class="label">地址标签：</div>
            <div class="field">
              <input v-model="formData.addressTags" class="input" placeholder="请输入地址标签，逗号分隔" />
            </div>
          </div>
        </div>
      </div>
    </div>
    <template v-slot:footer>
      <MyButton type="gray" style="margin-right:20px" @click="dialogVisible = false">取消</MyButton>
      <MyButton type="primary" @click="submit">确认</MyButton>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref, defineExpose, reactive, defineEmits } from 'vue'
import MyButton from '@/components/my-button'
import { editAddress, addAddress } from '@/api/order'
import City from '@/components/city.vue';
  const emit = defineEmits(['on-success'])
  const formData = reactive({
    id: '',
    receiver: '',
    contact: '',
    provinceCode: '',
    cityCode: '',
    countyCode: '',
    fullLocation: '',
    address: '',
    postalCode: '',
    addressTags: '',
    isDefault: 0
  })
  const dialogVisible = ref(false)
  // 打开对话框函数
  const open = (address) => {
    // 先填充数据 - 编辑
    if (address.id) {
      for (const key in formData) {
        formData[key] = address[key]
      }
    } else {
      // 先清空数据 - 添加
      for (const key in formData) {
        if (key !== 'isDefault') {
          formData[key] = ''
        }
      }
    }
    dialogVisible.value = true
  }
  // 提交操作
  const submit = () => {
    if (formData.id) {
      editAddress(formData).then(data => {
        // 修改成功
        dialogVisible.value = false
        emit('on-success', formData)
      })
    } else {
      addAddress(formData).then(data => {
        // 添加成功
        formData.id = data.result.id
        dialogVisible.value = false
        emit('on-success', formData)
      })
    }
  }
  // 选择地区
  const changeCty = (data) => {
    formData.provinceCode = data.provinceCode
    formData.cityCode = data.cityCode
    formData.countyCode = data.countyCode
    formData.fullLocation = data.fullLocation
  }
  defineExpose({
    open
  })
</script>
<style scoped lang='less'>
.wrapper {
  width: 100%;
  font-size: 14px;
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
  .xtx-form-item {
    display: flex;
    align-items: center;
    width: 700px;
    margin: 0 auto;
    padding-bottom: 25px;
    .label {
      width: 180px;
      padding-right: 10px;
      text-align: right;
      color: #999;
    }
    .field {
      margin-left: 0;
      width: 320px;
      height: 50px;
      position: relative;
      input {
        border: 1px solid #e4e4e4;
        width: 320px;
        height: 50px;
        line-height: 50px;
        padding: 0 10px;
      }
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>